package wzw.swing.calendar;

import java.awt.Dimension;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.kingcore.director.core.util.Lunar;
import com.sunking.swing.OpenSwingUtil;


/**
 * <p>Title: JDatePicker</p>
 * <p>Description:JDayLable 带选择日期功能的JLabel </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
 * @version 1.0
 */


public class DayLabel extends JLabel{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static ImageIcon todayIcon =
        OpenSwingUtil.getOpenSwingImage("today.gif", new ImageIcon());

    Date date = null;
    ImageIcon currentIcon = null;

    /**
     * 日期格式（TODAY/TIP用）
     */
    final SimpleDateFormat dateFormat
        = new SimpleDateFormat("yyyy/MM/dd");
    /**
     * 日格式
     */
    final SimpleDateFormat dayFormat = new SimpleDateFormat("d");

    public DayLabel(Date date){
        this(date, false);
    }

    /**
     * 
     * @param date
     * @param isSimplified 简化版
     */
    public DayLabel(Date date, boolean isSimplified){
        setPreferredSize(new Dimension(48, 18));
        setToolTipText(dateFormat.format(date));
        this.date = date;
        
        setHorizontalAlignment(JLabel.CENTER);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
//            setText(dayFormat.format(date));
		String text = "<html><center><font>"+dayFormat.format(date)+"</font></center>";
        String message = "<html><body><center>公元 "
				+ calendar.get(Calendar.YEAR) + "年"
				+ (calendar.get(Calendar.MONTH)+1) + "月"
				+ calendar.get(Calendar.DATE) + "日";
		if(isSimplified==false){
			Lunar lunar = new Lunar(calendar);
			text += "</br><center><font size='2'>"+lunar+"</font></center></html>";
			message += "<br>农历"+lunar.get_month()+"月"+ lunar.get_date()
					+ "&nbsp;&nbsp;&nbsp;&nbsp;(月"+lunar.get_Big_Or_Small()+")";
		}
        this.setText(text);
		this.setToolTipText(message);

        Date d = new Date();
        if(dateFormat.format(date).equals(dateFormat.format(d))){
            currentIcon = todayIcon;
        }
//        } else{
//            setText("Today:" + dateFormat.format(new Date()));
//            setIcon(todayIcon);
//            setHorizontalAlignment(JLabel.LEFT);
//        }
    }

    public Date getDate(){
        return date;
    }
    public void setDate(Date date){
        this.date = date;
    }
    public void paint(Graphics g){
        super.paint(g);
        if(currentIcon != null && isEnabled()){
            int x = (this.getWidth() - currentIcon.getIconWidth()) / 2;
            int y = (this.getHeight() - currentIcon.getIconHeight()) / 2;
            currentIcon.paintIcon(this, g, x, y);
        }
    }
}
